package comu.hnevc.zh.testdemo.fragment;

import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import comu.hnevc.zh.testdemo.R;
import comu.hnevc.zh.testdemo.dao.RechargeBean;
import comu.hnevc.zh.testdemo.dao.RechargeDao;

public class UserCenterFragment2 extends Fragment {
    private static final String TAG = "UserCenterFragment2";
    View view;
    ListView listView;
    MyUserCenterAdapter myBaseAdapter;
    RechargeDao carRechargeDao;
    List<RechargeBean> carRechargeInfos=new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.user_center_fragment2,container,false);
        carRechargeDao=new RechargeDao(getActivity());
        initView();
        return view;

    }

    private void initView() {
        listView=view.findViewById(R.id.user_center_fragment_listView);
        myBaseAdapter=new MyUserCenterAdapter();
        listView.setAdapter(myBaseAdapter);
        getRechargeRecord();
    }

    private void getRechargeRecord() {
         List<RechargeBean> rechargeBeans=carRechargeDao.queryByAll();
         if (rechargeBeans!=null){
             if (rechargeBeans.size()>0){
                 carRechargeInfos=rechargeBeans;
                 myBaseAdapter.setRechargeRecord(carRechargeInfos);
             }else {
                 Toast.makeText(getActivity(),"暂无记录",Toast.LENGTH_LONG).show();
             }
         }
    }

    public class MyUserCenterAdapter extends BaseAdapter {
        List<RechargeBean> mLists = new ArrayList<>();

        public void setRechargeRecord(List<RechargeBean> rechargeRecord) {
            this.mLists = rechargeRecord;
        }

        @Override
        public int getCount() {
            return mLists.size()==0?0:mLists.size();
        }

        @Override
        public Object getItem(int position) {
            return mLists.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @RequiresApi(api = Build.VERSION_CODES.M)
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView==null){
                convertView=View.inflate(getContext(),R.layout.user_center_item,null);
                viewHolder=new ViewHolder();
                viewHolder.findViewById(convertView);
                convertView.setTag(viewHolder);
            }else{
                viewHolder=(ViewHolder) convertView.getTag();
            }
            viewHolder.tv_c_time.setText(mLists.get(position).getList_time());
            String cweek=String.valueOf(Log.e("Cweek",mLists.get(position).getWeek()));
            if ("Monday".equals(cweek)){
                cweek="星期一";
            } else if ("Tuesday".equals(cweek)) {
                cweek="星期二";
            }else if ("Wednesday".equals(cweek)){
                cweek="星期三";
            }else if ("Thursday".equals(cweek)) {
                cweek = "星期四";
            } else if ("Friday".equals(cweek)) {
                cweek = "星期五";
            }else if ("Saturday".equals(cweek)) {
                cweek = "星期六";
            }else{
                cweek="星期天";
            }
            viewHolder.tv_c_week.setText(cweek);
            viewHolder.tv_recharger.setText("充值人"+mLists.get(position).getUser_name());
            viewHolder.tv_carnumber.setText("车牌号"+mLists.get(position).getCar_number());
            viewHolder.tv_recharge.setText(mLists.get(position).getRe_money());
            viewHolder.tv_balance.setText(mLists.get(position).getBalance());
            viewHolder.tv_recharge_time.setText(mLists.get(position).getRe_time());

            return convertView;
        }

         public  class ViewHolder{
            private TextView tv_c_time,tv_c_week;
            private TextView tv_recharger,tv_carnumber,tv_recharge,tv_balance,tv_recharge_time;
            void  findViewById(View viewItem){
                tv_c_time=viewItem.findViewById(R.id.tv_c_time);
                tv_c_week=viewItem.findViewById(R.id.tv_c_week);
                tv_recharger=viewItem.findViewById(R.id.tv_recharger);
                tv_carnumber=viewItem.findViewById(R.id.tv_carnumber);
                tv_recharge=viewItem.findViewById(R.id.tv_recharge);
                tv_balance=viewItem.findViewById(R.id.tv_balance);
                tv_recharge_time=viewItem.findViewById(R.id.tv_recharge_time);
            }
         }
    }
}

